package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.GoodsFirstGetTimeData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserGoodsFirstGetDataDaoImpl extends TempDataObjectDao<GoodsFirstGetTimeData> {
    public static String getGoodsResourDataSql = "select * from t_u_goods_get_time where userId=?";
    public Map<Integer,GoodsFirstGetTimeData> getGoodsResourData(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, GoodsFirstGetTimeData> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getGoodsResourDataSql);
            int i = 1;
            call.setLong(i++,userId);
            rs = call.executeQuery();
            while (rs.next()) {
                GoodsFirstGetTimeData goodsResourData = parse(rs);
                dataMap.put(goodsResourData.getGoodsId(), goodsResourData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String updateGoodsResourDataSql = "update t_u_goods_get_time set firstGetTime=? where userId=? and goodsId=?";
    @Override
    protected boolean updateData(GoodsFirstGetTimeData goodsResourData) {
        goodsResourData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, goodsResourData.getFirstGetTime()));
        para.put(i++, new DbParameter(Types.BIGINT, goodsResourData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, goodsResourData.getGoodsId()));
        if (getDb().executeNoneQuery(updateGoodsResourDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertGoodsResourDataSql = "insert into t_u_goods_get_time(userId,goodsId,firstGetTime) values (?,?,?)";
    @Override
    protected boolean addData(GoodsFirstGetTimeData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGoodsId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getFirstGetTime()));
        if (getDb().executeNoneQuery(insertGoodsResourDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected GoodsFirstGetTimeData parseData(ResultSet rs) throws SQLException {GoodsFirstGetTimeData data = new GoodsFirstGetTimeData();
        data.setUserId(rs.getLong("userId"));
        data.setGoodsId(rs.getInt("goodsId"));
        data.setFirstGetTime(rs.getLong("firstGetTime"));
        return data;
    }
}
